package com.woniuxy.gobang.ui;

import dao.UserDataDAO;
import entity.User;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * 注册页面
 * 
 * @author 张哲轩
 *
 */
public class LogInStage extends Stage {
	private Label lbAccount = new Label("账号");
	private TextField txtAcount = new TextField();
	private Label warnAccount = new Label("长度2-15");

	private Label lbPassword = new Label("密码");
	private PasswordField  txtPassword = new PasswordField();
	private Label warnPassword = new Label("长度6-20");

	private Label lbConfirm = new Label("确认密码");
	private PasswordField  txtConfirm = new PasswordField();
	private Label warnConfirm = new Label();

	private Stage stage = this;

	public LogInStage() {

		// 为窗体设置标题
		setTitle("注册页面");
		// 设置窗体大小不可调整
		setResizable(false);
		// 创建面板
		Pane pane = new Pane();
		// 账号位置
		lbAccount.setLayoutX(30);
		lbAccount.setLayoutY(20);
		txtAcount.setLayoutX(100);
		txtAcount.setLayoutY(20);
		txtAcount.setPrefWidth(180);
		warnAccount.setLayoutX(300);
		warnAccount.setLayoutY(20);

		// 密码位置
		lbPassword.setLayoutX(30);
		lbPassword.setLayoutY(60);
		txtPassword.setLayoutX(100);
		txtPassword.setLayoutY(60);
		txtPassword.setPrefWidth(180);
		warnPassword.setLayoutX(300);
		warnPassword.setLayoutY(60);
		// 确认密码位置
		lbConfirm.setLayoutX(30);
		lbConfirm.setLayoutY(100);
		txtConfirm.setLayoutX(100);
		txtConfirm.setLayoutY(100);
		txtConfirm.setPrefWidth(180);
		warnConfirm.setLayoutX(300);
		warnConfirm.setLayoutY(100);

		pane.getChildren().addAll(lbAccount, txtAcount, warnAccount, lbPassword, txtPassword, warnPassword, lbConfirm,
				txtConfirm, warnConfirm);

		// 确认按钮
		Button confirmBtn = new Button("注册");
		confirmBtn.setPrefSize(100, 30);
		confirmBtn.setLayoutX(50);
		confirmBtn.setLayoutY(150);
		pane.getChildren().add(confirmBtn);
		// 注册按钮事件
		confirmBtn.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				// 用于数据校验标志
				boolean bool = true;
				UserDataDAO userDAO =  new UserDataDAO();
				//清空提示信息
				warnAccount.setText("");
				warnPassword.setText("");
				warnConfirm.setText("");
				
				if(userDAO.isExist(txtAcount.getText())) {
					warnAccount.setTextFill(Color.RED);
					warnAccount.setText("账号已存在");
					bool = false;
					return;
				}
				
				if(!(validateAccount() && validatePwd() && validateCfmPwd())) {
					return;
				}
				

			
				if (bool) {
					User user = new User();
					user.setAccount(txtAcount.getText());
					user.setPassword(txtPassword.getText());
					try {
						userDAO.Insert(user);
						stage.close();		
						Global.logonStage.show();
					} catch (Exception e) {
						Alert alert = new Alert(AlertType.ERROR);
						alert.setContentText("注册失败！");
						alert.show();
					}
				}
			}
		});

		// 取消按钮
		Button cancelBtn = new Button("取消");
		cancelBtn.setPrefSize(100, 30);
		cancelBtn.setLayoutX(180);
		cancelBtn.setLayoutY(150);
		pane.getChildren().add(cancelBtn);
		// 取消按钮事件
		cancelBtn.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				stage.close();
			}
		});

		// 创建一个场景对象
		Scene scene = new Scene(pane, 500, 200);
		// 将场景添加到窗体上
		setScene(scene);
		// 将线条添加到面板
		show();
	}
	
	// 检查账户名长度
	private boolean validateAccount() {
		String account = txtAcount.getText();
		if (account.trim().equals("")) {
			warnAccount.setText("账号不能为空");
			warnAccount.setTextFill(Color.RED);
			return false;
		} else if (account.trim().length() < 2 || account.trim().length() > 15) {
			warnAccount.setText("账号长度在2-15之间");
			warnAccount.setTextFill(Color.RED);
			return false;
		} else {
			warnAccount.setText("账号格式有效");
			warnAccount.setTextFill(Color.GREEN);
			return true;
		}
	}
	
	private boolean validatePwd() {
		String password = txtPassword.getText();
		if (password.trim().equals("")) {
			warnPassword.setText("密码不能为空");
			warnPassword.setTextFill(Color.RED);
			return false;
		} else if (password.trim().length() < 6 || password.trim().length() > 20) {
			warnPassword.setText("密码长度不能小于6或大于20");
			warnPassword.setTextFill(Color.RED);
			return false;
		} else {
			warnPassword.setText("密码有效");
			warnPassword.setTextFill(Color.GREEN);
			return true;
		}
	}
	
	private boolean validateCfmPwd() {
		String password = txtPassword.getText();
		String cfmPwd = txtConfirm.getText();
		if (!password.equals(cfmPwd)) {
			warnConfirm.setText("确认密码与密码不一致");
			warnConfirm.setTextFill(Color.RED);
			return false;
		} else {
			warnConfirm.setText("");
			return true;
		}
	}
}
